﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using InfFront.InfoViews.Base;
using System.Windows.Data;
using System.Windows;

namespace InfFront.InfoViews.WPF
{
    public class BoolNotConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter,
                              System.Globalization.CultureInfo culture)
        {
            return !(bool)value;
        }

        public object ConvertBack(object value, Type targetType, object parameter,
                                  System.Globalization.CultureInfo culture)
        {
            return !(bool)value;
        }
    }

    public class BoolToVisibilityConverter : IValueConverter, IMultiValueConverter
    {
        public object Convert(object value, Type targetType, object parameter,
                              System.Globalization.CultureInfo culture)
        {
            if ((bool)value)
                return Visibility.Visible;
            return Visibility.Hidden;
        }

        public object ConvertBack(object value, Type targetType, object parameter,
                                  System.Globalization.CultureInfo culture)
        {
            System.Diagnostics.Debug.Assert(targetType.Equals(typeof(bool)));
            return ((Visibility)value) == Visibility.Visible;
        }

        public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            foreach (var val in values)
            {
                if (val == null || val.Equals(DependencyProperty.UnsetValue) || !(bool)val)
                    return Visibility.Hidden;
            }
            return Visibility.Visible;
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
        {
            var rv = new object[targetTypes.Length];
            for (int i = 0; i < rv.Length; ++i)
                rv[i] = ConvertBack(value, targetTypes[i], parameter, culture);
            return rv;
        }
    }

    public class EqualityCheckToBoolConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter,
                              System.Globalization.CultureInfo culture)
        {
            if (value == parameter)
                return true;
            if (value == null || parameter == null)
                return false;
            return value.Equals(parameter);
        }

        public object ConvertBack(object value, Type targetType, object parameter,
                                  System.Globalization.CultureInfo culture)
        {
            throw new NotSupportedException();
        }
    }

    public class InfoViewSourceSelectionCheckConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter,
                              System.Globalization.CultureInfo culture)
        {
            var view = (InfoViewSource)value;
            var settings = view.VisualSettings;
            var selectedPath = (string[])settings.GetValue(Settings.Manager.LAST_SELECTED_SOURCE_KEY);
            if (selectedPath == null)
                return false;
            var curPath = view.Path;
            if (selectedPath.Length != curPath.Length)
                return false;
            for (int i = 0; i < curPath.Length; ++i)
            {
                if (!curPath[i].Equals(selectedPath[i]))
                    return false;
            }
            return true;
        }

        public object ConvertBack(object value, Type targetType, object parameter,
                                  System.Globalization.CultureInfo culture)
        {
            throw new NotSupportedException();
        }
    }
    
    public class ColorTitleToColorConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter,
                              System.Globalization.CultureInfo culture)
        {
            var title = value as string;
            if (string.IsNullOrEmpty(title) || title.Contains('<'))
                return null;
            return System.Windows.Media.ColorConverter.ConvertFromString(title);
        }

        public object ConvertBack(object value, Type targetType, object parameter,
                                  System.Globalization.CultureInfo culture)
        {
            throw new NotSupportedException();
        }
    }

}
